package com.ztomorrow.ying_cun_plus.controller;

import com.ztomorrow.ying_cun_plus.auth.UserHolder;
import com.ztomorrow.ying_cun_plus.dto.req.UserCommentReqDto;
import com.ztomorrow.ying_cun_plus.dto.req.UserLoginReqDto;
import com.ztomorrow.ying_cun_plus.dto.req.UserRegisterReqDto;
import com.ztomorrow.ying_cun_plus.dto.resp.RestResp;
import com.ztomorrow.ying_cun_plus.dto.resp.UserLoginRespDto;
import com.ztomorrow.ying_cun_plus.dto.resp.UserRegisterRespDto;
import com.ztomorrow.ying_cun_plus.service.BookService;
import com.ztomorrow.ying_cun_plus.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/front/user")
public class UserController {

    private final UserService userService;
    private final BookService bookService;

    /**
     * 用户注册接口
     */
    @PostMapping("/register")
    public RestResp<UserRegisterRespDto> register(@RequestBody UserRegisterReqDto dto) {
        return userService.register(dto);
    }

    /**
     * 用户登录接口
     */
    @PostMapping("/login")
    public RestResp<UserLoginRespDto> login(@RequestBody UserLoginReqDto dto) {
        return userService.login(dto);
    }

    /**
     * 发表评论接口
     */
    @PostMapping("/comment")
    public RestResp<Void> comment( @RequestBody UserCommentReqDto dto) {
        dto.setUserId(UserHolder.getUserId());
        return bookService.saveComment(dto);
    }

    /**
     * 修改评论接口
     */
    @PutMapping("/comment/{id}")
    public RestResp<Void> updateComment(@PathVariable Long id,
                                        String content) {
        return bookService.updateComment(UserHolder.getUserId(), id, content);
    }

    /**
     * 删除评论接口
     */
    @DeleteMapping("/comment/{id}")
    public RestResp<Void> deleteComment(@PathVariable Long id) {
        return bookService.deleteComment(UserHolder.getUserId(), id);
    }
}
